<?php


namespace App\Models\Accident;

use App\Models\Model;
use App\Models\Workflow\WorkflowLog;
use App\Models\Workflow\WorkflowProcess;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Accident extends Model
{
    use SoftDeletes;

    protected $table = 'akbus_accident';

    protected $fillable = [
        'number',
        'case_number',
        'date',
        'license_plate_num',
        'user_id',
        'user_name',
        'accident_info',
        'accident_file',
        'receiver_id',
        'violation',
        'nature',
        'responsible',
        'car_damage',
        'three_car_damage',
        'injure_cost',
        'insurance',
        'total_cost',
        'balance_cost',
        'loss_cost',
        'litigation_cost',
    ];

    /**
     * 不可批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [
    ];

    /**
     * 类型转化
     *
     * @var array
     */
    protected $casts = [
    ];

    /**
     * 隐藏的字段
     *
     * @var array
     */
    protected $hidden = [
    ];

    /**
     * 获取表名
     *
     * @return mixed
     */
    public static function getTableName()
    {
        return with(new self)->getTable();
    }

    /**
     * 拼接车牌号字符
     *
     * @param  string  $value
     *
     * @return string
     */
    public function getLicensePlateNumAttribute($value): string
    {
        return '陕G'.$value;
    }

    /**
     * 人伤情况
     */
    public function injured(): HasMany
    {
        return $this->hasmany(AccidentInjured::class, 'accident_id');
    }

    /**
     * 资料详情
     */
    public function document(): HasMany
    {
        return $this->hasmany(AccidentDocument::class, 'accident_id');
    }

    /**
     * 维修详情
     */
    public function repair(): HasMany
    {
        return $this->hasmany(AccidentRepair::class, 'accident_id');
    }

    /**
     * 维修文件
     */
    public function repairFile(): HasMany
    {
        return $this->hasmany(AccidentRepairFile::class, 'accident_id');
    }

    /**
     *单据现在状态
     *
     * @return HasOne
     */
    public function process(): HasOne
    {
        return $this->hasOne(WorkflowProcess::class, 'apply_id');
    }

    /**
     * 单据审核历史记录
     *
     * @return HasMany
     */
    public function logs(): HasMany
    {
        return $this->hasmany(WorkflowLog::class, 'apply_id');
    }
}
